/**
 * @param {number[]} coins
 * @param {number} amount
 * @return {number}
 */
export var coinChange = function (coins, amount) {
  if (amount === 0) return 0
  const dp = new Array(amount + 1).fill(Infinity)
  dp[0] = 0
  for (let i = 0; i < coins.length; i++) {
    const n = coins[i]
    for (let j = n; j <= amount; j++) {
      dp[j] = Math.min(dp[j], dp[j - n] + 1)
    }
  }
  return dp[amount] !== Infinity ? dp[amount] : -1
}
